package com.javaweb.admin.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.javaweb.admin.entity.AnnualBudget;
import com.javaweb.admin.entity.NcTaskLog;
import com.javaweb.admin.entity.PriceVariNotice;
import com.javaweb.admin.entity.Scheduled;
import com.javaweb.admin.mapper.ScheduledMapper;
import com.javaweb.admin.service.*;
import com.javaweb.common.utils.DateUtils;
import com.sphsine.bip.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class UploadPriceNoticeJob extends AbstractJob {
    @Autowired
    INcTaskLogService iNcTaskLogService;
    @Autowired
    IPriceVariNoticeService priceVariNoticeService;
    @Autowired
    ScheduledMapper scheduledMapper;
    @Autowired
    IBIPService ibipService;
    @Autowired
    IBipServiceLogService bipServiceLogService;
    @Override
    public String getName() {
        QueryWrapper<Scheduled> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("classpath",this.getClass().getName());
        Scheduled entity = scheduledMapper.selectOne(queryWrapper);
        return entity.getName();
    }

    @Override
    public void execute() {

        String billNo = "";
        String body = "";
        List<PriceVariNotice> list= priceVariNoticeService.GetWSCList();
        logger.info(DateUtils.dateTimeNow() + "获取了" + list.size() + "个结算价记录");
        NcTaskLog taskLog = new NcTaskLog();
        taskLog.setTaskType("结算价");
        taskLog.setBillCount(list.size());
        taskLog.setStartTime(DateUtils.now());
        List<PriceVariNoticeData> budgetList = new ArrayList<>(1);
        PriceVariNoticeList postList = new PriceVariNoticeList();
        Integer id;
        if(list.size()>0) {
            PriceVariNotice budget;
            PriceVariNoticeData data;
            for (int i = 0; i < list.size(); i++) {
                id = list.get(i).getId();
                ibipService.sendNotice("",id);
//                djhs[0] = String.valueOf(list.get(i).getId());
//                budget = iAnnualBudgetService.GetDetail(id);
//                data = new AnnualBudgetData();
//                data.setYearly(budget.getYearly());
//                data.setFactoryOrg(budget.getSaleCode());
//                data.setPurchaseOrg(budget.getPurchaseCode());
//                data.setMaterial(budget.getMaterialCode());
//                data.setInternalPrice(budget.getInternalPrice()==null? BigDecimal.ZERO:budget.getInternalPrice());
//                data.setPurchaseNum(budget.getPurchaseNum());
//                data.setPurchaseAmount(budget.getPurchaseAmount());
//                data.setPurchaseMargin(budget.getPurchaseMargin());
//                data.setSalesUnitPrice(budget.getSalesUnitPrice()==null?BigDecimal.ZERO:budget.getSalesUnitPrice());
//                data.setSaleNum(budget.getSaleNum());
//                data.setSaleAmount(budget.getSaleAmount());
//                data.setSaleMargin(budget.getSaleMargin());
//                data.setVariableCost(budget.getVariableCost());
//                data.setBudgetState("0");
//                data.setImportMark("BS-"+DateUtils.getDate());
//
//
//
//                budgetList.add(data);
//                postList.setData(budgetList);
//
//                billNo=list.get(i).getMaterialCode();
//                body = JSON.toJSONString(postList, SerializerFeature.WriteBigDecimalAsPlain);
//                JsonResult result = ibipService.sendBudget(body, id);
//                budgetList.clear();
                //logger.info(DateUtils.dateTimeNow() + "提交了采购入库单：" + djhs[0]+",处理结果:"+result.getData().toString());
                logger.info(DateUtils.dateTimeNow() + "提交了结算价：" + id.toString());

            }
        }
        taskLog.setBillNo("");
        taskLog.setEndTime(DateUtils.now());
        iNcTaskLogService.save(taskLog);
        //System.out.println(DateUtils.dateTimeNow()+":上传了一条数据");
        this.cancel();
    }

}
